import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { CmtBaseCommunityApi } from '#/api/cmt/base/community';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { z } from '#/adapter/form';
import { getAreaTree } from '#/api/system/area';
import { getRangePickerDefaultProps } from '#/utils';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'code',
      label: '社区编码',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入社区编码',
      },
    },
    {
      fieldName: 'name',
      label: '社区名称',
      rules: 'required',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入社区名称',
      },
    },
    {
      fieldName: 'areaId',
      label: '所在地区',
      component: 'ApiTreeSelect',
      formItemClass: 'col-span-1',
      componentProps: {
        api: () => getAreaTree(),
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择省市区',
      },
    },
    {
      fieldName: 'street',
      label: '街道',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入街道',
      },
    },
    {
      fieldName: 'address',
      label: '详细地址',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        placeholder: '请输入详细地址',
      },
    },
    {
      fieldName: 'contactPerson',
      label: '联系人',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系人',
      },
    },
    {
      fieldName: 'contactPhone',
      label: '联系电话',
      component: 'Input',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'logo',
      label: '社区logo',
      component: 'ImageUpload',
      formItemClass: 'col-span-1',
    },
    {
      fieldName: 'sort',
      label: '排序',
      component: 'InputNumber',
      formItemClass: 'col-span-1',
      componentProps: {
        placeholder: '请输入排序',
        controlsPosition: 'right',
        class: '!w-full',
      },
      defaultValue: 0,
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'RadioGroup',
      formItemClass: 'col-span-2',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
      },
      rules: z.number().default(CommonStatusEnum.ENABLE).optional(),
    },
    {
      fieldName: 'introduction',
      label: '社区简介',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 3,
        placeholder: '请输入社区简介',
      },
    },
    {
      fieldName: 'remark',
      label: '备注',
      component: 'Input',
      formItemClass: 'col-span-2',
      componentProps: {
        type: 'textarea',
        rows: 2,
        placeholder: '请输入备注',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'code',
      label: '社区编码',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入社区编码',
      },
    },
    {
      fieldName: 'name',
      label: '社区名称',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入社区名称',
      },
    },
    {
      fieldName: 'areaId',
      label: '所在地区',
      component: 'ApiTreeSelect',
      componentProps: {
        api: () => getAreaTree(),
        labelField: 'name',
        valueField: 'id',
        childrenField: 'children',
        placeholder: '请选择省市区',
      },
    },
    {
      fieldName: 'street',
      label: '街道',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入街道',
      },
    },
    {
      fieldName: 'address',
      label: '详细地址',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入详细地址',
      },
    },
    {
      fieldName: 'contactPerson',
      label: '联系人',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入联系人',
      },
    },
    {
      fieldName: 'contactPhone',
      label: '联系电话',
      component: 'Input',
      componentProps: {
        clearable: true,
        placeholder: '请输入联系电话',
      },
    },
    {
      fieldName: 'status',
      label: '状态',
      component: 'Select',
      componentProps: {
        clearable: true,
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        placeholder: '请选择状态',
      },
    },
    {
      fieldName: 'createTime',
      label: '创建时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        clearable: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions<CmtBaseCommunityApi.BaseCommunity>['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'id',
      title: '社区ID',
      minWidth: 120,
    },
    {
      field: 'code',
      title: '社区编码',
      minWidth: 120,
    },
    {
      field: 'name',
      title: '社区名称',
      minWidth: 120,
    },
    {
      field: 'area',
      title: '所在地区',
      minWidth: 200,
      slots: { default: 'area' },
    },
    {
      field: 'address',
      title: '详细地址',
      minWidth: 120,
    },
    {
      field: 'contactPerson',
      title: '联系人',
      minWidth: 120,
    },
    {
      field: 'contactPhone',
      title: '联系电话',
      minWidth: 120,
    },
    {
      field: 'logo',
      title: '社区logo',
      minWidth: 100,
      cellRender: {
        name: 'CellImage',
        props: { size: 60 },
      },
    },
    {
      field: 'introduction',
      title: '社区简介',
      minWidth: 120,
    },
    {
      field: 'sort',
      title: '排序',
      minWidth: 120,
    },
    {
      field: 'status',
      title: '状态',
      minWidth: 120,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'remark',
      title: '备注',
      minWidth: 120,
    },
    {
      field: 'createTime',
      title: '创建时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      title: '操作',
      width: 200,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}

